package com.liuqing.www.http程序读取;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class HttpReadChuhan {
public static int num=0;
//public static Map<String,String> xx=new HashMap<String,String>();
private static String title="";
//public static String path="http://192.168.1.34:8080/chuhan/index.action";
public static String path="http://www.fkjava.com/article/2/2946607646684759670.html";
private static String ContentType="Content-Type";
private static String Content="application/x-www-form-urlencoded;charset=GBK";
static{
//for(int i=0;i<10;i++){
//	title+=title;
//}
//xx.put("title", title);
//xx.put("textContent", title);
//xx.put("editor", "");
//xx.put("htmlContent", title);
System.getProperties().put("proxySet","true");

System.getProperties().setProperty("http.proxyHost", "218.26.204.66");
System.getProperties().setProperty("http.proxyPort", "8080");

}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
	try {
		ExecutorService threadpool=Executors.newCachedThreadPool()  ;//始终只有2个线程在运行
		for(int i=0;i<100;i++){
			mMyThread t=new mMyThread(i+"");
			threadpool.execute(t);
		}
//		 System.out.println(HttpRead.URLPost("http://www.fkjava.com/qa-save.htm",HttpRead.xx));
	} catch (Exception e) {
	// TODO Auto-generated catch block
	e.printStackTrace();
}
	}
	private static final String URL_PARAM_CONNECT_FLAG = "&";
	/**
	 * url为地址，map装的为要传递的参数
	 * @param strUrl
	 * @param map
	 * @return
	 * @throws IOException
	 */
	public static List<String> URLGet(String strUrl,Map<String,String> map) throws IOException{
		if(strUrl==null){
			return null;
		}
		String strtTotalURL = "";
		List<String> list = new ArrayList<String>();
		if(map==null){
			strtTotalURL=strUrl;
		}else{
			strtTotalURL = strUrl+"?"+getUrl(map);
		}
//		System.out.println(strtTotalURL);
		URL url = new URL(strtTotalURL);
		HttpURLConnection con = (HttpURLConnection)url.openConnection();
		con.setUseCaches(false);//浏览器是否缓存页面
		con.setFollowRedirects(true);//支持服务器器重定向
		BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(),"utf8"));
		while(true){
			String line = in.readLine();
			if(line ==null){
				break;
			}
			else list.add(line);
		}
		
		in.close();
		return list;
	}
	
	public static List<String> URLPost(String strUrl,Map<String,String> map) throws IOException{
		String content = "";
		content = getUrl(map);
		
		URL url = new URL(strUrl);
		HttpURLConnection con = (HttpURLConnection)url.openConnection();
		con.setDoInput(true);//URL 连接可用于输入和/或输出。如果打算使用 URL 连接进行输入，则将 DoInput 标志设置为 true；如果不打算使用，则设置为 false。默认值为 true。 
		con.setDoOutput(true);
		con.setAllowUserInteraction(true);
		con.setUseCaches(false);
		con.setRequestMethod("POST");
		con.setRequestProperty(ContentType, Content);
		BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(con.getOutputStream()));
		bw.write(content);
		bw.flush();
		bw.close();
		BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
		List<String> list = new ArrayList<String>();
		while (true) {
			String line = br.readLine();
			if (line == null) {
				break;
			} else if (line.equals("")) {
				continue;
			} else {
				list.add(line);
			}
		}
		System.gc();
		return list;
	
	}
	/**
	 * 重写url用？xx=xx
	 * @param map
	 * @return
	 */
	static String getUrl(Map<String,String> map){
		if(map == null||map.size()==0){
			return("");
		}
		StringBuffer url = new StringBuffer();
		Set<String> keys = map.keySet();
		for(Iterator i = keys.iterator();i.hasNext();){
			String key = String.valueOf(i.next());
			Object val  =map.get(key);
			String str  =val!=null?val.toString():"";
			try {
				str = URLEncoder.encode(str,"GBK");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			url.append(key).append("=").append(str).append(URL_PARAM_CONNECT_FLAG);
		}
		String strURL ="";
		strURL = url.toString();
		
		if(URL_PARAM_CONNECT_FLAG.equals(""+strURL.charAt(strURL.length()-1))){
			strURL = strURL.substring(0,strURL.length()-1);
		}
		return strURL;
	}
	
}
class mMyThread extends Thread{
	public mMyThread(String name){
//		setName(name);
		super(name);
	}
	public void run(){//重新run方法
		
		while(true){
			 try {
				 if(HttpReadChuhan.num<200){
					 System.out.println("准备进入........");
					 String url=null;
						List<String> str=HttpReadChuhan.URLGet("http://192.168.1.200:8080/chuhan/login!list.action?lid=1234&sid=1234&psd=1234&loadtime=1302071923408&area=%E9%A3%8E%E6%9A%B4%E4%B8%80%E5%8C%BA200&operation=0",null);
						A:for(String st:str){
							String[] ss=st.split("</a>");
							for(String s1:ss){
								if(s1.indexOf(">进入游戏")>0){
									url="http://192.168.1.200:8080"+s1.substring(s1.indexOf("<a href=")+9,s1.indexOf(">进入游戏")-1);
									break A;
								}
							}
						}
						System.out.println("@@@@@@@@@@@@@@@@@@@"+url);
						List<String> strs=HttpReadChuhan.URLGet(url,null);
						if(strs==null||strs.size()==0){
							 System.out.println("**********错误");
						}
						HttpReadChuhan.num+=1;
						 System.out.println("成功进入第"+HttpReadChuhan.num+"个角色");
				 }

				 Thread.sleep(500);
				 System.gc();
		} catch (Exception e) {
			// TODO Auto-generated catch block
//			e.printStackTrace();
		}
		}
	}
}